import os, sys, tempfile, copy, re
import subprocess

class sfprops:
    def __init__(self, filename):
        self.filename = filename

        print(filename)

        """
        Properties of /Users/jm/Documents/Projects/pycdp/examples/beat_0000.wav:
        File type:  soundfile
        Format         :  Standard WAVE format
        Sample Rate    :  44100
        Channels       :  2
        Sample Frames  :  142713
        sample type:   :  24bit (packed)
        duration       :  3.2361 secs
        no peak data in this infile
        """


        """
        ('Process:', 'EXTRACT OR CONVERT CHANNELS OF SOUNDFILE.')
        Properties of /var/folders/HZ/HZ12kO5AGsyXNbCNnLiM+++++TM/-Tmp-/tmpv7hGII/beat_0000.wav:
        File type:  soundfile
        Format         :  Standard WAVE format
        Sample Rate    :  44100
        Channels       :  2
        Sample Frames  :  142713
        sample type:   :  24bit (packed)
        duration       :  3.2361 secs
        no peak data in this infile
        """
        if os.path.exists(filename):
            self.props_re= {'File type':'file_type',
                             'Format' :'format',
                             'Sample Rate':'sample_rate',
                             'Channels' :'channels',
                             'Sample Frames':'sample_frames',
                             'sample_type' : 'sample type',
                             'duration' : 'duration'}

            try:
                if os.path.exists(filename):
                    everything = subprocess.getoutput("sfprops {}".format(filename))
                else:
                    print("no file {}".format(filename))
            except Exception as e:
               raise

            print(self.props_re.items())

            #self.props_re = {str(key) : re.compile(value) for key, value in self.props_re.items()}

            self.props = {}

            print("ERGESX")

            everything = everything.split("\n")

            for line in everything:
                output_key = line.split(':')
                if len(output_key) is 2:
                    key = output_key[0].strip()
                    value = output_key[1].strip()
                    if key in self.props_re.keys():
                        print(key, value)
                        self.props[self.props_re[key]] = output_key[1].strip()
                    """
                    if output_key[0] in self.props_re:
                        key = self.props_re[output_key[0]]
                        self.props[key] = output_key[1]
                    """


                #print("Line in everything:\n{}".format(line))
                #print("\n{} = [}".format(line.spit("\n")))
                """
                for propwhut, propname in self.props_re:
                    if self.props_rel[
                    print("\n{} {}".format(propwhut, match))
                    if match:
                        self.props[propname] = match.group(1).strip()
                        print(matcH)
                    else:
                        self.props[propname] = "Unknown"
                """

            print(self.props)
            print(self.props_re)

            if self.props["file_type"] is "soundfile":
                self.props["duration_ms"] = float(self.props["sample_frames"]) / float(self.props["sample_rate"]) * 1000

                    #else:
                        #print line
            #print self.props
        else:
            print("{} does not exist.".format(filename))
